/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#include <decls.hpp>

#include <cubefs/api/metadata.hpp>

#include "exceptions.hpp"


JNIEXPORT jlong JNICALL METADATA_PKG(BitmapField, GetBits) (JNIEnv* env, jclass, jlong me, long pageh, jint idx, jint nbits, jboolean abi) {
    CUBEFS_EXC_BEGIN

    BitmapField *field = Handle2Ptr<BitmapField>(me);
    Page* page = Handle2Ptr<Page>(pageh);

    return field->GetBits(page, idx, nbits, abi);

    CUBEFS_EXC_HANDLE
    return 0;
}

JNIEXPORT void JNICALL METADATA_PKG(BitmapField, SetBits) (JNIEnv* env, jclass, jlong me, long pageh, jlong bits, jint idx, jint nbits, jboolean abi) {
    CUBEFS_EXC_BEGIN

    BitmapField *field = Handle2Ptr<BitmapField>(me);
    Page* page = Handle2Ptr<Page>(pageh);

    field->SetBits(page, bits, idx, nbits, abi);

    CUBEFS_EXC_HANDLE
}



JNIEXPORT void JNICALL METADATA_PKG(NumberField, GetValue__JJIJZ) (JNIEnv* env, jclass, jlong me, jlong pageh, jint idx, jlong num, jboolean abi) {
    CUBEFS_EXC_BEGIN

    NumberField *field = Handle2Ptr<NumberField>(me);
    Page* page = Handle2Ptr<Page>(pageh);
    Number* number = Handle2Ptr<Number>(num);

    field->GetValue(page, idx, number, abi);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT jlong JNICALL METADATA_PKG(NumberField, GetValue__JJIZ) (JNIEnv* env, jclass, jlong me, jlong pageh, jint idx, jboolean abi) {
    CUBEFS_EXC_BEGIN

    NumberField *field = Handle2Ptr<NumberField>(me);
    Page* page = Handle2Ptr<Page>(pageh);
    
    return field->GetValue(page, idx, abi);

    CUBEFS_EXC_HANDLE
    return 0;
}


JNIEXPORT void JNICALL METADATA_PKG(NumberField, SetValue) (JNIEnv* env, jclass, jlong me, long pageh, jint idx, jlong num, jboolean abi) {
    CUBEFS_EXC_BEGIN

    NumberField *field = Handle2Ptr<NumberField>(me);
    Page* page = Handle2Ptr<Page>(pageh);
    Number* number = Handle2Ptr<Number>(num);

    field->SetValue(page, idx, number, abi);

    CUBEFS_EXC_HANDLE
}


JNIEXPORT void JNICALL METADATA_PKG(NumberField, SetValueLong) (JNIEnv* env, jclass, jlong me, long pageh, jint idx, jlong num, jboolean abi) {
    CUBEFS_EXC_BEGIN

    NumberField *field = Handle2Ptr<NumberField>(me);
    Page* page = Handle2Ptr<Page>(pageh);

    field->SetValue(page, idx, num, abi);

    CUBEFS_EXC_HANDLE
}



JNIEXPORT void JNICALL METADATA_PKG(IDField, GetValue) (JNIEnv* env, jclass, jlong me, long pageh, jint idx, jlong num, jboolean abi) {
    CUBEFS_EXC_BEGIN

    IDField *field = Handle2Ptr<IDField>(me);
    Page* page = Handle2Ptr<Page>(pageh);
    TypedIDValue* number = Handle2Ptr<TypedIDValue>(num);

    field->GetValue(page, idx, number, abi);

    CUBEFS_EXC_HANDLE
}

JNIEXPORT void JNICALL METADATA_PKG(IDField, SetValue) (JNIEnv* env, jclass, jlong me, long pageh, jint idx, jlong num, jboolean abi) {
    CUBEFS_EXC_BEGIN

    IDField *field = Handle2Ptr<IDField>(me);
    Page* page = Handle2Ptr<Page>(pageh);
    TypedIDValue* number = Handle2Ptr<TypedIDValue>(num);

    field->SetValue(page, idx, number, abi);

    CUBEFS_EXC_HANDLE
}
